package com.wlz.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONObject;
import com.wlz.util.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.checkerframework.checker.units.qual.C;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wlz
 * @date 2023-06-09  15:09
 */
@Slf4j
public class TaskTest {

    private static final String FILE_URL = "/Users/wlz/Desktop/ARCFOX-αT进行中任务任务分组被过滤的零部件.xlsx";

    public static void main(String[] args) {
        List<Map<String,Object>> list = getPart(FILE_URL);
        System.out.println(list);

        if (CollUtil.isNotEmpty(list)) {
            // 通过hutool工具类创建writer，默认创建xls格式
            ExcelWriter writer = ExcelUtil.getWriter("/Users/wlz/Desktop/ARCFOX-αT进行中任务任务分组被过滤的零部件-1.xlsx");
            //将list map数据写出文件
            writer.write(list, true);
            writer.close();
        }
    }

    public static List<Map<String,Object>> getPart(String fileUrl) {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(fileUrl));
        List<Map<String,Object>> vinList = getPartList(reader);
        reader.close();
        return  vinList;
    }


    public static List<Map<String,Object>> getPartList(ExcelReader reader) {
        List<Sheet> sheets = reader.getSheets();
        List<Map<String,Object>> list  = new ArrayList<>();
        for (Sheet sheet : sheets) {
            String sheetName = sheet.getSheetName();
            reader.setSheet(sheet);
            List<Map<String, Object>> maps = reader.readAll();
            for (Map<String, Object> map : maps) {
                String s = (String) map.get("被过滤掉的ICC或TBX件");
                JSONObject object = JSONObject.parseObject(s);
                String softwareVersion = object.getString("softwareVersion");
                String targetSoftwareVersion = object.getString("targetSoftwareVersion");
                if (!softwareVersion.equalsIgnoreCase(targetSoftwareVersion)) {
                    map.put("零部件名称", object.getString("partName"));
                    map.put("零部件号", object.getString("partNumber"));
                    map.put("原版本", object.getString("softwareVersion"));
                    map.put("目标版本", object.getString("targetSoftwareVersion"));
                    map.remove("被过滤掉的ICC或TBX件");
                    list.add(map);
                }
            }
        }
        return list;
    }

}
